<?php

namespace app\api\controller\user;

use app\api\controller\Base;
use app\common\service\user\LoginService;
use app\common\service\user\UserService;

class User extends Base
{
    /**
     * Author:Zhongyuan Lu
     * 设置密码
     * @return mixed|void
     */
    public function setPassword()
    {
        if (!$this->request->isPost()) {
            return $this->result([], 400, '请求方式错误');
        }

        $uid = $this->request->uid;

        $password = $this->request->post('password', '', 'trim');
        $rpassword = $this->request->post('password', '', 'trim');

        if (empty($uid) || $uid < 1 || empty($password) || strlen($password) !== 6) {
            return $this->result([], 400, '参数错误');
        }

        if (!empty($rpassword)) {
            if ($password !== $rpassword) {
                return $this->result([], 400, '两次密码不一致');
            }
        }

        $params['password'] = md5(md5($password));

        $data = UserService::updateOne($uid, $params);

        return $this->res($data);
    }

    public function setAvatar()
    {
        if (!$this->request->isPost()) {
            return $this->result([], 400, '请求方式错误');
        }
        $uid = $this->request->post('id',0,'int');
        $params['avatar'] = $this->request->post('avatar', '', 'trim');

        if (empty($uid) || $uid < 1 || empty($params['avatar']) || !isImage($params['avatar'])) {
            return $this->result([], 400, '参数错误');
        }

        $data = UserService::updateOne($uid,$params);

        return $this->res($data);
    }

    public function shareInvitationCode()
    {
        $uid = $this->request->uid;
        $data = UserService::shareInvitationCode($uid);
        return $data;
    }

    public function userInfo()
    {
        $uid = $this->request->uid;
        $integral = \app\admin\model\User::where('id',$uid)->field('integral')->find();
        if ($integral){
            $data = ['code' => 200 ,'message' => '获取成功','data'=>$integral];
            return $this->res($data);
        }else{
            $data = ['code' => 400 ,'message' => '获取失败','data'=>''];
            return $this->res($data);
        }
    }

    public function live()
    {
        $params['live_id'] = 1;
        $params['goods_id'] = 13;
        $params['num'] = 3;
        event('LiveGoodsSales',$params);
    }

    /**
     * Author:Yingbo Sun
     * 获取用户信息
     * @return mixed|void
     */
    public function getUserInfo()
    {
        $uid = $this->request->uid;
        if(empty($uid)){
            return $this->apiFail('参数错误');
        }
        $user = UserService::getUserInfo($uid);
        return $this->res($user);
    }
}






